package com.ucar.train.service.impl;

import com.github.pagehelper.PageHelper;
import com.ucar.train.dao.RoleAndMenuMapper;
import com.ucar.train.dao.RoleMapper;
import com.ucar.train.dao.UserMapper;
import com.ucar.train.dao.UserRoleMapper;
import com.ucar.train.dto.MenuDTO;
import com.ucar.train.dto.RoleAndMenuDTO;
import com.ucar.train.dto.RoleDTO;
import com.ucar.train.service.RoleService;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.utils.MailUtils;
import com.ucar.train.vo.Role;
import com.ucar.train.vo.RoleAndMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhoujinmu
 * @title RoleServiceImpl
 * @projectName train-parent
 * @description TODO
 * @created 2019-07-24 12:26
 **/
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleAndMenuMapper roleAndMenuMapper;

    @Override
    public List<RoleAndMenuDTO> getRoleAndMenu(){
        List<RoleAndMenuDTO> roleAndMenus = roleMapper.getRoleAndMenu();
        return roleAndMenus;
    }


    @Override
    public List<RoleDTO>  getRoleList(Integer page,Integer pageSize,String orderBy){
        PageHelper.startPage(page, pageSize,orderBy);
        return roleMapper.getAllRole();
    }


    @Override
    public void addMenuToRole(Long rid, Long mid) {
        List<RoleAndMenu> roleAndMenus = new ArrayList<>();
        RoleAndMenu roleAndMenu1 = new RoleAndMenu();
        roleAndMenu1.setRid(rid);
        roleAndMenu1.setMid(mid);
        roleAndMenu1.setModifyTime(JodaUtils.nowAsDate());
        roleAndMenu1.setCreateTime(JodaUtils.nowAsDate());
        roleAndMenus.add(roleAndMenu1);
        roleAndMenuMapper.insertRoleAndMenus(roleAndMenus);
    }

    @Override
    public void updateRoleAndMenu(Long rid,Long mid) {
        List<RoleAndMenu> roleAndMenus = new ArrayList<>();
        RoleAndMenu roleAndMenu = new RoleAndMenu();
        roleAndMenu.setRid(rid);
        roleAndMenu.setMid(mid);
        roleAndMenu.setCreateTime(JodaUtils.nowAsDate());
        roleAndMenu.setModifyTime(JodaUtils.nowAsDate());
        roleAndMenuMapper.updateRoleAndMenus(roleAndMenus);
    }

    @Override
    public void deleteMenuForRole(Long rid, Long mid) {
        roleAndMenuMapper.deleteRoleAndMenus(rid,mid);
    }

    @Override
    public void deleteRole(Long rid) {
        roleMapper.deleteRole(rid);
        roleAndMenuMapper.deleteRoleAllMenus(rid);
    }

    @Override
    public void addRole(Role role,String createOperator) {
        List<Role> roles = new ArrayList<>();
        role.setCreateOperator(createOperator);
        role.setModifyOperator(createOperator);
        role.setCreateTime(JodaUtils.nowAsDate());
        role.setModifyTime(JodaUtils.nowAsDate());
        roles.add(role);
        roleMapper.insertRole(role);
    }

    @Override
    public void updateRole(Role role,String modifyOperator) {
        role.setModifyOperator(modifyOperator);
        role.setModifyTime(JodaUtils.nowAsDate());
        roleMapper.updateRole(role);
    }

    @Override
    public RoleDTO getRoleById(Long id) {
        RoleDTO role = roleMapper.getRoleById(id);
        return role;
    }

  @Override
  public List<MenuDTO> selectPermission(Long id) {
    return roleAndMenuMapper.selectRoleAndMenu(id);
  }

  @Override
  public List<RoleDTO> getByGroupId(Long groupId) {
    return roleMapper.getByGroupId(groupId);
  }

  //    @Override
//    public void sendEmail() {
//        String mailSubject = ;
//        String mailContent = ;
//         MailUtils.creditMail()
//    }

  @Override
  public List<RoleDTO> getNoPermission() {
    return roleMapper.getNoPermission();
  }
}
